﻿using BLL.UserInfos.DTO;
using DAL.Userinfos;
using Model.UserInfos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.UserInfos
{
    public class UserInfoBll : IUserInfoBll
    {
        private readonly IUserinfoDal userinfoDal;

        public UserInfoBll(IUserinfoDal userinfoDal)
        {
            this.userinfoDal = userinfoDal;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public RestFullDto AddUserInfo(AddUserInfoDto addUserInfoDto)
        {
            //判断用户名是否重复
            var list = userinfoDal.GetUserName(addUserInfoDto.UserName);

            //判断确认密码是否与密码一致
            if (addUserInfoDto.UserPassword == addUserInfoDto.Password2)
            {
                if (list == null)
                {
                    UserInfo userInfo = new UserInfo();
                    userInfo.UserName = addUserInfoDto.UserName;
                    userInfo.UserPassword = addUserInfoDto.UserPassword;
                    userInfo.UserPhone = addUserInfoDto.UserPhone;
                    userInfo.Name = addUserInfoDto.Name;
                    userInfo.Email = addUserInfoDto.Email;
                    userinfoDal.Create(userInfo);

                    return new RestFullDto { Code = 1, Message = "恭喜您，注册成功！" };
                }
                else
                {
                    return new RestFullDto { Code = 2, Message = "用户名/手机号/邮箱已存在，请重新输入！" };
                }
            }
            else
            {
                return new RestFullDto { Code = 3, Message = "确认密码与密码不一致，请重新输入！" };
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginDto"></param>
        /// <returns></returns>
        public RestDto<LoginDto> GetLogin(LoginDto loginDto)
        {
            //判断用户名是否重复
            var list = userinfoDal.GetUserName(loginDto.UserName);

            if (list == null)
            {
                return new RestDto<LoginDto> { Code = 1, Message = "该用户名/手机号/邮箱不存在，请重新输入！" };
            }
            else
            {
                if (loginDto.UserPassword == list.UserPassword)
                {
                    return new RestDto<LoginDto> { Code = 2, Message = "恭喜您，登陆成功！" };
                }
                else
                {
                    return new RestDto<LoginDto> { Code = 3, Message = "密码错误，请重新输入！" };
                }
            }
        }
    }
}
